/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import Action.AdminSessionBeanLocal;
import Entity.Admin;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cyrax
 */
@ManagedBean
@SessionScoped
public class AdminBean {

    @EJB
    private AdminSessionBeanLocal adminSessionBean;
    /** Creates a new instance of AdminBean */
    private List<Admin> admin;
    private Admin selectedAdmin;
    private Admin adm;
    private String msg;
    private String msg2;
    private String msg3;
    static InitialContext cnt = null;

    public AdminBean() {
        adm = new Admin();
    }

    //Action Admin
    //List all admin
    public List<Admin> getListAdmin() {
        try {
            admin = adminSessionBean.listAllAdmin();
            System.out.println("hello:" + admin);

        } catch (Exception e) {
            System.out.println("Eror list book : " + e.getMessage());
        }
        return admin;
    }

    public String loginUser() {
        ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();
        try {

            cnt = new InitialContext();

            FacesContext face = FacesContext.getCurrentInstance();
            Iterator stIterator = adminSessionBean.listAllAdmin().iterator();
            while (stIterator.hasNext()) {
                Admin ad = (Admin) stIterator.next();

                if (ad.getUserName().equals(getAdm().getUserName()) && ad.getPassword().equals(getAdm().getPassword())) {
                    HttpSession session = (HttpSession) face.getExternalContext().getSession(true);
                    session.setAttribute("userid", ad.getUserName());
                    session.setAttribute("fullName", ad.getFullName());

                    extcontext.redirect("Admin.xhtml");
                    System.out.println("==========================Test===" + ad.getUserName());

                }



            }
            setMsg3("Enter username or password fail");
            return "LoginAdmin";


        } catch (Exception ex) {
            System.out.println("==========================Loi2===");
            return "false";
        }
    }

    public void logout() {
        ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();

        try {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
            if (session.getAttribute("username") == null) {

                extcontext.redirect("LoginAdmin.xhtml");
            } else {
                session.invalidate();
                extcontext.redirect("LoginAdmin.xhtml");
            }

        } catch (Exception e) {
        }
    }

    public String logoutuser() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        if (session.getAttribute("username") == null) {

            return "Alogin";
        } else {
            session.invalidate();
            return "fasle";
        }
    }

    public void insertAdmin() {


        try {

            if (adm.getUserName() == null) {
                System.out.println("User name is not null!!!");

            } else {
                adminSessionBean.addAdmin(adm);
            }

        } catch (Exception ex) {
            System.out.println("Error : " + ex.getMessage());
        }
    }

    public void updateAdmin() {
        try {

            String name = selectedAdmin.getUserName();
            adminSessionBean.updateAd(name, getSelectedAdmin().getPassword(), getSelectedAdmin().getFullName(), getSelectedAdmin().getAddress(), getSelectedAdmin().getPhone(), getSelectedAdmin().getEmail(), getSelectedAdmin().getBirthday());
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public void deleteAdmin(Admin adm) {
        try {

            adminSessionBean.deleteAdmin(adm);
        } catch (Exception e) {
            System.out.println("Error : " + e.getMessage());
        }
    }

    /**
     * @return the ad
     */
    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * @return the adm
     */
    public Admin getAdm() {
        return adm;
    }

    /**
     * @param adm the adm to set
     */
    public void setAdm(Admin adm) {
        this.adm = adm;
    }

    /**
     * @return the selectedAdmin
     */
    public Admin getSelectedAdmin() {
        return selectedAdmin;
    }

    /**
     * @param selectedAdmin the selectedAdmin to set
     */
    public void setSelectedAdmin(Admin selectedAdmin) {
        this.selectedAdmin = selectedAdmin;
    }
    private String key;
    private String searchType;

    /**
     * @return the key
     */
    public String getKey() {
        return key;
    }

    /**
     * @param key the key to set
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     * @return the searchType
     */
    public String getSearchType() {
        return searchType;
    }

    /**
     * @param searchType the searchType to set
     */
    public void setSearchType(String searchType) {
        this.searchType = searchType;
    }

    public List<Admin> getsearchAdmin() {

//        if (searchType.equals("admin")) {

        admin = adminSessionBean.searchAdmin(key.toString());

//        }
        System.out.println("List" + admin);
        return admin;

    }

    public void checkAdminUserName() {
        if (adminSessionBean.checkUserAdmin(adm.getUserName())) {

            msg = "This User Name is already existed";
        } else {
            msg = "";

        }

    }
    public void checkAdminEmail()
    {
       if (adminSessionBean.checkEmail(adm.getEmail())) {

            msg2 = "This Email is already existed";
        } else {
            msg2 = "";

        }

    }

    /**
     * @return the msg2
     */
    public String getMsg2() {
        return msg2;
    }

    /**
     * @param msg2 the msg2 to set
     */
    public void setMsg2(String msg2) {
        this.msg2 = msg2;
    }

    /**
     * @return the msg3
     */
    public String getMsg3() {
        return msg3;
    }

    /**
     * @param msg3 the msg3 to set
     */
    public void setMsg3(String msg3) {
        this.msg3 = msg3;
    }
}
